package class01;

/**
 * @description: 选择排序
 * @time: 2021/7/2 14:40
 * @author: lhy
 */
public class SelectSort {
    public static void selectSort(int[] arr){
        if (arr == null || arr.length < 2) {
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            int minValueIndex = i;
            for (int j = i + 1; j < arr.length; j++) {
                minValueIndex = arr[j] < arr[minValueIndex] ? j : minValueIndex;
            }
            swap(arr,i,minValueIndex);
        }
    }

    private static void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    public static void printArray(int[] arr){
        for (int value : arr) {
            System.out.print(value + " ");
        }
        System.out.println();
    }



    public static void main(String[] args) {
        int[] arr = {1,4,9,2,4,6,2,45,33};
        printArray(arr);
        selectSort(arr);
        printArray(arr);
    }
}
